$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/fyparent/list',
        datatype: "json",
        colModel: [			
			{ label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true,hidden:true },
            { label: '用户名', name: 'userName', index: 'user_name', width: 80 },
			{ label: '性别', name: 'sex', index: 'sex', width: 60,formatter: function(item, index) {
					if (item == 1) {
                        return '<span class="label label-warning">男</span>';
                    }else if (item == 2) {
                        return '<span class="label label-success">女</span>';
                    }else{
                        return '<span class="label label-info">保密</span>';
					}
                } },
			{ label: '生日', name: 'birthday', index: 'birthday', width: 80 },
            { label: '邮件', name: 'email', index: 'email', width: 80 },
			{ label: '注册时间', name: 'regTime', index: 'reg_time', width: 80 }, 			
			{ label: '最后登录时间', name: 'lastLogin', index: 'last_login', width: 90 },
			{ label: '最后登录ip', name: 'lastIp', index: 'last_ip', width: 80 }, 			
			{ label: '手机号码', name: 'mobile', index: 'mobile', width: 80 }, 			
			{ label: '是否验证手机', name: 'mobileValidated', index: 'mobile_validated', width: 80 }, 			
			{ label: '第三方来源', name: 'oauth', index: 'oauth', width: 80 },
			{ label: '头像', name: 'headPic', index: 'head_pic', width: 70,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.headPic!=null&&rowdata.headPic!=''){
                        return '<img src='+rowdata.headPic+' width="50" height="50"/>';
                    }else{
                        return '';
                    }


                } },
			{ label: '学生姓名', name: 'studentName', index: 'student_name', width: 80 },
			{ label: '关系', name: 'relation', index: 'relation', width: 80 }, 			
			{ label: '学生班级', name: 'studentClassId', index: 'student_class_id', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var classname = '' ;
                    if(rowdata.studentClassId!=null&&rowdata.studentClassId!=''){
                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/fyclasstable/info/"+rowdata.studentClassId,
                            success: function(r){
                                classname = r.fyClassTable.classname;

                            }
                        });
					}
                    return classname;
                } },
			{ label: '学生学校', name: 'schoolId', index: 'school_id', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var schoolname  = '';
                    if(rowdata.schoolId!=null && rowdata.schoolId!=''){
                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/fyschool/info/"+rowdata.schoolId,
                            success: function(r){
                                schoolname = r.fySchool.tname;

                            }
                        });
					}

                    return schoolname;
                } }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            username:null
		},
		showList: true,
		title: null,
		fyUsers: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fyUsers = {};
		},
		update: function (event) {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(userId)
		},
		saveOrUpdate: function (event) {
			var url = vm.fyUsers.userId == null ? "sys/fyusers/save" : "sys/fyusers/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.fyUsers),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/fyusers/delete",
                    contentType: "application/json",
				    data: JSON.stringify(userIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(userId){
			$.get(baseURL + "sys/fyusers/info/"+userId, function(r){
                vm.fyUsers = r.fyUsers;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'username': vm.q.username},
                page:page
            }).trigger("reloadGrid");
		}
	}
});